package com.lyhengtongwl.zqsnews.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.BaseEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.DataCheckUtils;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;
import com.lyhengtongwl.zqsnews.utils.ToastUtil;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

/**
 * Created by Steven on 2019/1/22.
 */
@SuppressLint("Registered")
public class LoginAndRegisterActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.tv_login)
    TextView tv_login;
    public static Activity mAct = null;

    @Override
    public void setListen() {
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String editStr = et_phone.getText().toString();
                if (editStr.length() > 0) {
                    tv_login.setBackgroundResource(R.drawable.bg_btn);
                } else {
                    tv_login.setBackgroundResource(R.drawable.bg_btn_gray);
                }
            }
        });
    }

    @Override
    public void setTitleBarColor() {
        new TitleBuilder(this).setTitleText("登录/注册")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_loginregister;
    }

    @Override
    public void initView() {
        mAct = this;
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.tv_login, R.id.iv_wxlogin})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_login://点击下一步  校验是否存在 该用户
                String phone = et_phone.getText().toString();
                if (DataCheckUtils.isCellphone(et_phone.getText().toString())) {
                    checkUser(phone);
                } else {
                    ToastUtil.showShort(App.getContext(), "手机号不正确！");
                }
                break;
            case R.id.iv_wxlogin:
                wxLogin();
                break;
        }
    }

    //校验用户是否存在
    private void checkUser(final String phone) {
        tv_login.setClickable(false);
        Task.getApiService().accountInit(phone).enqueue(new MyCallback<BaseEntity>(LoginAndRegisterActivity.this) {
            @Override
            protected void onSuccess(Response<BaseEntity> response) {
                tv_login.setClickable(true);
                //把token保存在本地
                if (("0").equals(response.body().getCode())) {
                    //表示存在该用户  跳转登录页面
                    finish();
                    startActivity(new Intent(LoginAndRegisterActivity.this, LoginActivity.class)
                            .putExtra("phone", phone));
                } else if (("100").equals(response.body().getCode())) {
                    finish();
                    //表示该用户不存在  跳转  注册页面
                    startActivity(new Intent(LoginAndRegisterActivity.this, RegisterActivity.class)
                            .putExtra("phone", phone));
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }


    /**
     * 微信登录
     */
    private void wxLogin() {
        if (!App.getWXAPI().isWXAppInstalled()) {
            ToastUtil.showShort(this, "请安装微信");
        } else {
            final SendAuth.Req req = new SendAuth.Req();
            req.scope = "snsapi_userinfo";
            req.state = "pintuan";
            App.getWXAPI().sendReq(req);
        }
    }


}
